﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Alerts_Administration_2010.interfaces;

namespace Alerts_Administration_2010.classes
{
    public class OverviewItemBLL
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string AlertType { get; set; }
        public string EventType { get; set; }
        public string List { get; set; }
        public string UserName { get; set; }
    }
    public class DetailsViewItemBLL
    {
        private Dictionary<string, string> _eventTypes = new Dictionary<string, string>();
        private Dictionary<string, string> _alertFrequencies = new Dictionary<string, string>();
        private Dictionary<string, string> _temlateFilters = new Dictionary<string, string>();
        public string Id { get; set; }
        public string Title { get; set; }
        public string EMail { get; set; }
        public string AlertType { get; set; }
        public string ChangeType { get; set; }
        public string EventType { get; set; }
        public string Filter { get; set; }
        public string List { get; set; }
        public string UserName { get; set; }
        public string AlertFrequency { get; set; }
        public DateTime AlertTime { get; set; }
        public Dictionary<string, string> EventTypes { get { return _eventTypes; } }
        public Dictionary<string, string> AlertFrequencies { get { return _alertFrequencies; } }
        public Dictionary<string, string> TemplateFilters { get { return _temlateFilters; } }
    }


    public class AlertsBLL
    {
        public List<OverviewItemBLL> GetAlerts()
        {
            List<OverviewItemBLL> result = new List<OverviewItemBLL>();

            foreach (SPAlert a in SPContext.Current.Web.Alerts)
            {
                OverviewItemBLL item = CreateOverviewItemBll(a);
                result.Add(item);
            }

            return result;
        }
        public DetailsViewItemBLL GetAlertById(string Id)
        {
            if (Id == null) return null;
            SPAlert alert = AlertWrapper.GetAlertById(SPContext.Current.Web, new Guid(Id));
            return CreateDetailsViewItemBll(alert);
        }
        public void UpdateAlert(string EventType, string Title, string AlertFrequency, DateTime AlertTime, string Filter, string Id)
        {
            AlertWrapper alertWrapper = new AlertWrapper(SPContext.Current.Web, Id);
            SPEventType sPEventType = (SPEventType)Enum.Parse(typeof(SPEventType), EventType);
            SPAlertFrequency sPAlertFrequency = (SPAlertFrequency)Enum.Parse(typeof(SPAlertFrequency), AlertFrequency);
            alertWrapper.Alert.Title = Title;
            alertWrapper.Alert.EventType = sPEventType;
            switch (alertWrapper.Alert.EventType)
            {
                case SPEventType.Add:
                    alertWrapper.Alert.Properties["eventtypeindex"] = "1";
                    break;
                case SPEventType.All:
                    alertWrapper.Alert.Properties["eventtypeindex"] = "0";
                    break;
                case SPEventType.Delete:
                    alertWrapper.Alert.Properties["eventtypeindex"] = "3";
                    break;
                case SPEventType.Discussion:
                    alertWrapper.Alert.Properties["eventtypeindex"] = "4";
                    break;
                case SPEventType.Modify:
                    alertWrapper.Alert.Properties["eventtypeindex"] = "2";
                    break;
                default:
                    break;
            }
            foreach (IAlertTemplateFilter filter in alertWrapper.TemplateFilterList)
            {
                if (filter.Index.ToString() == Filter)
                {
                    alertWrapper.Alert.Filter = filter.QueryNode.OuterXml;
                    alertWrapper.Alert.Properties["filterindex"] = filter.Index.ToString();
                    break;
                }
            }
            alertWrapper.Alert.AlertFrequency = sPAlertFrequency;
            switch (alertWrapper.Alert.AlertFrequency)
            {
                case SPAlertFrequency.Daily:
                case SPAlertFrequency.Weekly:
                    alertWrapper.Alert.AlertTime = AlertTime;
                    break;
            }
            alertWrapper.Alert.Update();
        }
        private static OverviewItemBLL CreateOverviewItemBll(SPAlert a)
        {
            if (a == null) return null;
            OverviewItemBLL item = new OverviewItemBLL();
            item.Id = a.ID.ToString();
            item.Title = a.Title;
            item.AlertType = a.AlertType.ToString();
            item.EventType = a.EventType.ToString();
            item.List = a.List.Title;
            item.UserName = a.User.Name;
            return item;
        }
        private static DetailsViewItemBLL CreateDetailsViewItemBll(SPAlert a)
        {
            if (a == null) return null;
            DetailsViewItemBLL item = new DetailsViewItemBLL();
            item.Id = a.ID.ToString();
            item.Title = a.Title;
            item.AlertType = a.AlertType.ToString();
            item.EventType = a.EventType.ToString();
            item.List = a.List.Title;
            item.UserName = a.User.Name;
            item.AlertFrequency = a.AlertFrequency.ToString();
            if (a.AlertFrequency != SPAlertFrequency.Immediate) item.AlertTime = a.AlertTime;
            
            item.EventTypes.Add(SPEventType.All.ToString(), "All changes");
            item.EventTypes.Add(SPEventType.Add.ToString(), "New items are added");
            item.EventTypes.Add(SPEventType.Modify.ToString(), "Existing items are modified");
            item.EventTypes.Add(SPEventType.Delete.ToString(), "Items are deleted");
            item.EventTypes.Add(SPEventType.Discussion.ToString(), "Web discussion updates");

            item.AlertFrequencies.Add(SPAlertFrequency.Immediate.ToString(), "Send e-mail immediatetly");
            item.AlertFrequencies.Add(SPAlertFrequency.Daily.ToString(), "Send a daily summary");
            item.AlertFrequencies.Add(SPAlertFrequency.Weekly.ToString(), "Send a weekly summary");

            var w = new AlertWrapper(a);
            
            foreach (IAlertTemplateFilter filter in w.TemplateFilterList)
            {
                item.TemplateFilters.Add(filter.Index.ToString(), filter.FriendlyName);
            }

            item.Filter = w.SelectedTemplateFilter.Index.ToString();

            return item;
        }
    }
}
